package net.cmbt.okframe.okbanner;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.cmbt.okframe.R;

/**
 * Created by LG on 2019/12/10.
 */

public class OkIndicatorNormal extends LinearLayout implements SendAttributeListener {

    private View indicator_group;
    Context context;
    private OKBannerNormal oKBannerNormal;
    private OKBannerNormal okBannerClass;

    public OkIndicatorNormal(Context context) {
        super(context);
    }

    public OkIndicatorNormal(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }


    public OkIndicatorNormal(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER);
        setBackgroundColor(Color.WHITE);
        this.context = context;
    }


    @Override
    public void sendTotalCount(int num) {

        for (int i = 0; i < num; i++) {
            TextView textView = new TextView(context);
            LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layout.setMargins(0, 0, 20, 0);
            textView.setLayoutParams(layout);
            if (i == 0) {
                textView.setWidth(32);
                textView.setHeight(22);
                textView.setBackgroundResource(R.drawable.red_round);
            } else {
                textView.setWidth(22);
                textView.setHeight(22);
                textView.setBackgroundResource(R.drawable.gay_round);
            }
            addView(textView);
        }
    }

    @Override
    public void sendCurrentPostion(int postion) {

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            TextView childAt = (TextView) getChildAt(i);
            if (postion != i) {
                childAt.setWidth(22);
                childAt.setHeight(22);
                childAt.setBackgroundResource(R.drawable.gay_round);
            } else {
                childAt.setWidth(32);
                childAt.setHeight(22);
                childAt.setBackgroundResource(R.drawable.red_round);
            }
        }

    }

    //设置监听
    public void setOkBannerClass(OKBannerNormal okBannerClass) {
        this.okBannerClass = okBannerClass;
        okBannerClass.setSendAttributeListener(this);
    }
}
